﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TimKen.DataAccess;
using System.Data.SqlClient;
using TimKen.App;
using TimKen.DataEntity;
using Common;

namespace TimKen.Web.Admin.Customer
{
    public partial class Dealer_Manage : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.AspNetPager1.RecordCount = GetTotal();
            if (!IsPostBack)
            {
                Bind();
            }

        }
        protected void Bind()
        {
            DataSet ds = LoadData("View_Dealer", "ProvinceID", "ASC", 1, 10, "");
            this.gvDealerList.DataSource = ds.Tables[0].DefaultView;
            this.gvDealerList.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtDealerName.Text) && !string.IsNullOrEmpty(this.txtLicensedNo.Text))
            {
                this.gvDealerList.DataSource = LoadData("View_Dealer", "ProvinceID", "ASC", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, "and LicensedNo like '%" + this.txtLicensedNo.Text + "%'  and dealerName like '%" + this.txtDealerName.Text + "%' ");
                this.gvDealerList.DataBind();
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.gvDealerList.DataSource = LoadData("View_Dealer", "ProvinceID", "ASC", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, "");
            this.gvDealerList.DataBind();
        }
        protected void gvDealerList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                string sql = "delete from Dealer  WHERE DealerID={0}";
                sql = string.Format(sql, int.Parse(e.CommandArgument.ToString()));
                DataSet ds = DbHelperSQL.Query(sql);
                Bind();
            }
            else if (e.CommandName == "mod")
            {
                string scr = "window.open('DealerManageDetail.aspx?DealerID='+" + e.CommandArgument + ", '','newwindow', 'height=100, width=300, top=0, left=0, titlebar=no,toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" + scr + "</script>");
            }
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string scr = "window.open('DealerManageDetail.aspx', '','newwindow', 'height=100, width=300, top=0, left=0, titlebar=no,toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" + scr + "</script>");
        }
        public DataSet LoadData(string tableName, string columName, string sort, int pageIndex, int pageSize, string condition)
        {
            SqlParameter[] sqlParameters = new SqlParameter[6];
            sqlParameters[0] = new SqlParameter("@TableName", SqlDbType.VarChar, 20);
            sqlParameters[0].Value = tableName;
            sqlParameters[1] = new SqlParameter("@PkColumName", SqlDbType.VarChar, 100);
            sqlParameters[1].Value = columName;
            sqlParameters[2] = new SqlParameter("@Sort", SqlDbType.VarChar, 4);
            sqlParameters[2].Value = sort;
            sqlParameters[3] = new SqlParameter("@PageIndex", SqlDbType.Int);
            sqlParameters[3].Value = pageIndex;
            sqlParameters[4] = new SqlParameter("@PageSize", SqlDbType.Int);
            sqlParameters[4].Value = pageSize;
            sqlParameters[5] = new SqlParameter("@Condition", SqlDbType.NVarChar, 400);
            sqlParameters[5].Value = condition;
            return SqlHelper.ExecuteDataset(DBSetting.ConText, CommandType.StoredProcedure, "SPS_GetProDataByPageIndex", sqlParameters);
        }
        public int GetTotal()
        {
            string cmdText = "select COUNT(*) from View_Dealer where 1=1";
            if (!string.IsNullOrEmpty(this.txtDealerName.Text))
            {
                cmdText += "and dealerName like '%" + this.txtDealerName.Text + "%'";
            }
            if (!string.IsNullOrEmpty(this.txtLicensedNo.Text))
            {
                cmdText += "and LicensedNo like '%" + this.txtLicensedNo.Text + "%'";
            }
            return Convert.ToInt32(SqlHelper.ExecuteScalar(DBSetting.ConText, CommandType.Text, cmdText));
        }
    }
}
